package com.qf.myschool.servlet;

import com.qf.myschool.entity.Student;
import com.qf.myschool.service.IStudentService;
import com.qf.myschool.service.impl.StudentServiceImpl;
import com.qf.myschool.tool.PageTool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 老师的操作的Servlet
 */
@WebServlet("/TeacherServlet")
public class TeacherServlet extends SuperServlet {
	private static final long serialVersionUID = 1L;

	/**
	*创建学生数据访问对象
	*/
	IStudentService stuService=new StudentServiceImpl();



	private String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("flag", "add");
		return "register.jsp";
	}


	/**
	 * 删除学生信息
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private String delStudentByName(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
		/*1.接收请求中数据*/
		//根据前端的window.location="DelStudentByNameServlet?uname="+uname1;这句代码中Key
		String uname=request.getParameter("uname");
		
		/*调用相应业务层处理请求,并接收结果*/
		int result=stuService.delStudentByName(uname);
		return"TeacherServlet?method=allStudent";
		/*第一种删除
		response.getWriter().write(String.valueOf(result));
		*/
		/*3.根据结果跳转*/
		//response.sendRedirect("TeacherServlet?method=allStudent");
		//doAllStudent(request, response);
	}

	/**
	 * 分页查询所有学生信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private String allStudent(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.接收请求中数据,省略
		String currpage=request.getParameter("currpage");//传过来页码
		//初始化分页工具类的对象的属性
		PageTool p1=new PageTool();

		/*2.调用相应业务层处理请求,用变量接收处理结果*/
		List<Student> stus=stuService.queryStudent(p1,currpage);
		
		/*3.根据结果跳转页面*/
		//将所有学生信息存请求对象中
		request.setAttribute("stuList", stus);
		//将分页对象中信息存在请求对象中
		request.setAttribute("page1",p1);

		if(currpage==null){
			return "studentList.jsp";
		}else{
			return "modleList.jsp";
		}

	}


}
